﻿Public Class DISCHARGE
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_MASDISCHARGE As New PROVIDERSCLASS

    Private Sub DISCHARGE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridDischarge()

        DesignForm(Me)
        DesignGrid(MASDISCHARGEGrid)

        PROVIDER_MASDISCHARGE.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridDischarge()
        Dim objSelectDataMASDISCHAGRE As New masdischarge
        Dim MASDISCHAGRE As DataTable
        MASDISCHAGRE = connect.GetTable(objSelectDataMASDISCHAGRE.selectMasdischarge_)
        MASDISCHARGEGrid.PrimaryGrid.DataSource = MASDISCHAGRE
    End Sub


    Private Sub MASDISCHARGEGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASDISCHARGEGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส Discharge").Value() IsNot DBNull.Value Then
            dischargeIDTextbox.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส Discharge").Value()
        Else
            dischargeIDTextbox.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("Discharge").Value() IsNot DBNull.Value Then
            dischargeDescriptionTextbox.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("Discharge").Value()
        Else
            dischargeDescriptionTextbox.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            dischargeStatusCheckbox.Checked = True
        Else
            dischargeStatusCheckbox.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE masdischarge SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `dischgid` = '" & dischargeIDTextbox.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
        'connectlocal = conlocal.NewConnection()
        'If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
        '    Try
        '        Dim Sql As String
        '        'connectlocal.BeginTrans()
        '        Sql = "UPDATE MASDISCHARGE SET "
        '        If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
        '            Sql += "[STATUS] = 1 "
        '        Else
        '            Sql += "[STATUS] = 0 "
        '        End If
        '        Sql += "WHERE [DISCHGID] = '" & dischargeIDTextbox.Text & "' ;"

        '        'connectlocal.ExecuteNonQuery(Sql)
        '        'connectlocal.CommitTrans()
        '    Catch ex As Exception
        '        connectlocal.RollbackTrans()
        '        MsgBox(ex.ToString)
        '    End Try
        'End If
        'connectlocal.Dispose()
        addMasdischarge.Enabled = False
    End Sub

    Private Sub addMasdischarge_Click(sender As Object, e As EventArgs) Handles addMasdischarge.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASDISCHARGE.PADD_ = True Then
                Dim objInsertData As New masdischarge
                'objInsertData.tambonTMBID_ = CInt(codeTambonTextbox.Text)
                objInsertData.masdischargeDescription_ = dischargeDescriptionTextbox.Text
                objInsertData.masdischargeStatus_ = dischargeStatusCheckbox.Checked
                objInsertData.insertMasdischargeData()

                SelectDatagridDischarge()

                dischargeDescriptionTextbox.Text = ""
                dischargeIDTextbox.Text = ""
                dischargeStatusCheckbox.Checked = False
            Else
                MsgBox(PROVIDER_MASDISCHARGE.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMasdischarge_Click(sender As Object, e As EventArgs) Handles editMasdischarge.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASDISCHARGE.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูล Discharge ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    Dim objEditData As New masdischarge
                    objEditData.masdischargeID_ = CInt(dischargeIDTextbox.Text)
                    objEditData.masdischargeDescription_ = dischargeDescriptionTextbox.Text
                    objEditData.masdischargeStatus_ = dischargeStatusCheckbox.Checked
                    objEditData.editMasdischargeData()

                    SelectDatagridDischarge()

                    dischargeDescriptionTextbox.Text = ""
                    dischargeIDTextbox.Text = ""
                    dischargeStatusCheckbox.Checked = False
                End If
                addMasdischarge.Enabled = True
            Else
                MsgBox(PROVIDER_MASDISCHARGE.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(masdischarge.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(masdischarge.Text)
        nextform.ShowDialog()
    End Sub
End Class